﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNShapeEditor
{
    /// <summary>
    /// Manages actions. Actions are classes derived from the Action base class. These
    /// along with this manager support the command-pattern.
    /// </summary>
    public class ActionManager
    {
        #region Public methods

        /// <summary>
        /// Constructor.
        /// </summary>
        public ActionManager()
        {
        }

        /// <summary>
        /// Adds an action to the collection we manage.
        /// </summary>
        public void addAction(Action action)
        {
            // We add it to the undo stack...
            m_UndoStack.Push(action);
        }

        /// <summary>
        /// Undoes the action at the top of the undo-stack, and moves it to the redo-stack.
        /// </summary>
        public void undo()
        {
            // Are there any items on the undo stack?
            if (m_UndoStack.Count == 0)
            {
                return;
            }
            Action action = m_UndoStack.Pop();
            action.undo();
            m_RedoStack.Push(action);
        }

        /// <summary>
        /// redoes the action at the top of the redo-stack, and moves it to the undo-stack.
        /// </summary>
        public void redo()
        {
            // Are there any items on the redo stack?
            if (m_RedoStack.Count == 0)
            {
                return;
            }
            Action action = m_RedoStack.Pop();
            action.redo();
            m_UndoStack.Push(action);
        }

        #endregion

        #region Private data

        // Collection of actions on the undo-stack...
        private Stack<Action> m_UndoStack = new Stack<Action>();

        // Collection of actions on the redo-stack...
        private Stack<Action> m_RedoStack = new Stack<Action>();

        #endregion
    }
}
